import Vue from "vue";
import Vuex from "vuex";
import axios from "axios";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    // 标签  如果localStorage里没有tags就默认["默认","衣","食","住","行"]
    tags: window.localStorage.getItem("tags") ? JSON.parse(window.localStorage.getItem("tags")) : ["默认", "衣", "食", "住", "行"],
    // 记账的数据
    tallyData: [],
  },
  getters: {
    // 标签
    getTags(state) {
      return state.tags;
    },
    // 获取记账数据
    getTallyData(state) {
      return state.tallyData;
    }
  },
  mutations: {
    // 更新标签
    setTags(state, data) {
      state.tags = data;
      window.localStorage.setItem("tags", JSON.stringify(data));
    },
    // // 更新记账数据
    // setTallyData(state, data) {
    //   let newData = [data, ...state.tallyData];
    //   state.tallyData = newData;
    //   window.localStorage.setItem("tallyData", JSON.stringify(newData));
    // },
    // // 删除记账的某一条数据
    // updateTallyData(state, data) {
    //   state.tallyData = data;
    //   window.localStorage.setItem("tallyData", JSON.stringify(data));
    // }
  },
  actions: {
    getTallyDataFromServer(context) {
      axios({
        method: 'get',
        url: 'http://localhost:8888/tally/gettally',
      }).then(
        (res) => {
          context.state.tallyData = res.data;
        }
      )
    },
    deleteTallyDataFromServer(context, id) {

      axios({
        method: 'post',
        url: 'http://localhost:8888/delete/tally/deletetally',
        data: {
          id: id
        }
      }).then(
        (res) => {
          console.log(res.data)
          // context.state.tallyData = res.data;
        }
      )
    },
    addTallyDataFromServer(context, data) {
      console.log(data)
      axios({
        method: 'post',
        url: 'http://localhost:8888/add/tally/addtally',
        data: {
          data: data
        }
      }).then(
        (res) => {
          //console.log(res.data)
          // context.state.tallyData = res.data;
        }
      )
    }
  },



});
